﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DAL;
using DTO;
using System.Security.Cryptography;

namespace BLL
{
    public class UserBLL
    {
        private static int userID = 0;
        GaraDataContext DB = new GaraDataContext();

        public IQueryable SelectAll()
        {
            return (from user in DB.Users
                        join role in DB.Roles
                        on user.RoleID equals role.RoleID into result
                        from li in result.DefaultIfEmpty()
                        select new
                        {
                            user.UserID,
                            user.StaffID,
                            li.RoleName,
                            user.Status
                        }
                       ).Distinct().AsQueryable();                       
        }

        public string Insert(UserDTO userDTO)
        {
            try
            {
                User user = new User();
                user.UserID = userID++;
                user.StaffID = userDTO.StaffID;
                user.RoleID = userDTO.RoleID;
                user.Password = getMd5Hash(userDTO.Password);
                user.Status = userDTO.Status;

                DB.Users.InsertOnSubmit(user);
                DB.SubmitChanges();
                return MessageAction.Success;
            }
            catch (Exception ex)
            {
                return ex.Message;
            }
        }

        public static string getMd5Hash(string input)
        {
            // Create a new instance of the MD5CryptoServiceProvider object.
            MD5 md5Hasher = MD5.Create();

            // Convert the input string to a byte array and compute the hash.
            byte[] data = md5Hasher.ComputeHash(Encoding.Default.GetBytes(input));

            // Create a new Stringbuilder to collect the bytes
            // and create a string.
            StringBuilder sBuilder = new StringBuilder();

            // Loop through each byte of the hashed data 
            // and format each one as a hexadecimal string.
            for (int i = 0; i < data.Length; i++)
            {
                sBuilder.Append(data[i].ToString("x2"));
            }

            // Return the hexadecimal string.
            return sBuilder.ToString();
        }

        public bool CheckLogin(string username, string password)
        {
            string hashpass;
            hashpass = getMd5Hash(password);
            int count = DB.Users.Where(s => s.StaffID == username && s.Password == hashpass && s.Status == "Cấp phép").Count();
            if (count > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        public string Update(UserDTO userDTO)
        {
            try
            {
                User user = DB.Users.Where(n => n.UserID == userDTO.UserID).FirstOrDefault();
                user.StaffID = userDTO.StaffID;
                user.Password = getMd5Hash(userDTO.Password);
                user.RoleID = userDTO.RoleID;
                user.Status = userDTO.Status;

                DB.SubmitChanges();
                return MessageAction.Success;
            }
            catch (Exception ex)
            {
                return ex.Message;
            }

        }

    }
}
